/**
 * 计时器工具类
 * 用于游戏中的时间计算
 */
class Timer {
    constructor() {
        // 开始时间
        this.startTime = 0;
        
        // 暂停时间
        this.pauseTime = 0;
        
        // 累计暂停时间
        this.totalPausedTime = 0;
        
        // 计时器状态
        this.isRunning = false;
        this.isPaused = false;
    }
    
    /**
     * 开始计时
     */
    start() {
        if (!this.isRunning) {
            this.startTime = Date.now();
            this.totalPausedTime = 0;
            this.isRunning = true;
            this.isPaused = false;
        }
    }
    
    /**
     * 停止计时
     */
    stop() {
        this.isRunning = false;
        this.isPaused = false;
    }
    
    /**
     * 暂停计时
     */
    pause() {
        if (this.isRunning && !this.isPaused) {
            this.pauseTime = Date.now();
            this.isPaused = true;
        }
    }
    
    /**
     * 恢复计时
     */
    resume() {
        if (this.isRunning && this.isPaused) {
            // 计算暂停时间
            this.totalPausedTime += Date.now() - this.pauseTime;
            this.isPaused = false;
        }
    }
    
    /**
     * 重置计时器
     */
    reset() {
        this.startTime = 0;
        this.pauseTime = 0;
        this.totalPausedTime = 0;
        this.isRunning = false;
        this.isPaused = false;
    }
    
    /**
     * 获取经过的时间（秒）
     * @returns {number} 经过的时间（秒）
     */
    getElapsedTime() {
        if (!this.isRunning) {
            return 0;
        }
        
        if (this.isPaused) {
            return (this.pauseTime - this.startTime - this.totalPausedTime) / 1000;
        }
        
        return (Date.now() - this.startTime - this.totalPausedTime) / 1000;
    }
} 